home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / str2key.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  5KB  |  161 lines

  1. /* lib/des/str2key.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include "des_locl.h"
  49.  
  50. extern int des_check_key;
  51.  
  52. void des_string_to_key(str, key)
  53. char *str;
  54. des_cblock (*key);
  55.     {
  56.     des_key_schedule ks;
  57.     int i,length;
  58.     register unsigned char j;
  59.  
  60.     memset(key,0,8);
  61.     length=strlen(str);
  62. #ifdef OLD_STR_TO_KEY
  63.     for (i=0; i<length; i++)
  64.         (*key)[i%8]^=(str[i]<<1);
  65. #else /* MIT COMPATIBLE */
  66.     for (i=0; i<length; i++)
  67.         {
  68.         j=str[i];
  69.         if ((i%16) < 8)
  70.             (*key)[i%8]^=(j<<1);
  71.         else
  72.             {
  73.             /* Reverse the bit order 05/05/92 eay */
  74.             j=((j<<4)&0xf0)|((j>>4)&0x0f);
  75.             j=((j<<2)&0xcc)|((j>>2)&0x33);
  76.             j=((j<<1)&0xaa)|((j>>1)&0x55);
  77.             (*key)[7-(i%8)]^=j;
  78.             }
  79.         }
  80. #endif
  81.     des_set_odd_parity((des_cblock *)key);
  82.     i=des_check_key;
  83.     des_check_key=0;
  84.     des_set_key((des_cblock *)key,ks);
  85.     des_check_key=i;
  86.     des_cbc_cksum((des_cblock *)str,(des_cblock *)key,(long)length,ks,
  87.         (des_cblock *)key);
  88.     memset(ks,0,sizeof(ks));
  89.     des_set_odd_parity((des_cblock *)key);
  90.     }
  91.  
  92. void des_string_to_2keys(str, key1, key2)
  93. char *str;
  94. des_cblock (*key1);
  95. des_cblock (*key2);
  96.     {
  97.     des_key_schedule ks;
  98.     int i,length;
  99.     register unsigned char j;
  100.  
  101.     memset(key1,0,8);
  102.     memset(key2,0,8);
  103.     length=strlen(str);
  104. #ifdef OLD_STR_TO_KEY
  105.     if (length <= 8)
  106.         {
  107.         for (i=0; i<length; i++)
  108.             {
  109.             (*key2)[i]=(*key1)[i]=(str[i]<<1);
  110.             }
  111.         }
  112.     else
  113.         {
  114.         for (i=0; i<length; i++)
  115.             {
  116.             if ((i/8)&1)
  117.                 (*key2)[i%8]^=(str[i]<<1);
  118.             else
  119.                 (*key1)[i%8]^=(str[i]<<1);
  120.             }
  121.         }
  122. #else /* MIT COMPATIBLE */
  123.     for (i=0; i<length; i++)
  124.         {
  125.         j=str[i];
  126.         if ((i%32) < 16)
  127.             {
  128.             if ((i%16) < 8)
  129.                 (*key1)[i%8]^=(j<<1);
  130.             else
  131.                 (*key2)[i%8]^=(j<<1);
  132.             }
  133.         else
  134.             {
  135.             j=((j<<4)&0xf0)|((j>>4)&0x0f);
  136.             j=((j<<2)&0xcc)|((j>>2)&0x33);
  137.             j=((j<<1)&0xaa)|((j>>1)&0x55);
  138.             if ((i%16) < 8)
  139.                 (*key1)[7-(i%8)]^=j;
  140.             else
  141.                 (*key2)[7-(i%8)]^=j;
  142.             }
  143.         }
  144.     if (length <= 8) memcpy(key2,key1,8);
  145. #endif
  146.     des_set_odd_parity((des_cblock *)key1);
  147.     des_set_odd_parity((des_cblock *)key2);
  148.     i=des_check_key;
  149.     des_check_key=0;
  150.     des_set_key((des_cblock *)key1,ks);
  151.     des_cbc_cksum((des_cblock *)str,(des_cblock *)key1,(long)length,ks,
  152.         (des_cblock *)key1);
  153.     des_set_key((des_cblock *)key2,ks);
  154.     des_cbc_cksum((des_cblock *)str,(des_cblock *)key2,(long)length,ks,
  155.         (des_cblock *)key2);
  156.     des_check_key=i;
  157.     memset(ks,0,sizeof(ks));
  158.     des_set_odd_parity(key1);
  159.     des_set_odd_parity(key2);
  160.     }
  161.